<?php
/**
 * 后台用户组控制器
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/11/29 11:55
 * @copyright  deyi.com
 */

namespace Apps\Acp\Controllers;

use Apps\Acp\Models\Manager;
use Apps\Acp\Models\ManagerGroup;
use Apps\Acp\Services\ManagerGroupService;
use Apps\Common\Constant\AcpConstant;
use Apps\Common\Constant\BusinessConstant;
use Apps\Common\Controller\AcpBaseController;
use Deyi\Functions\PageFunction;

/**
 * Class GroupController
 * @package Apps\Acp\Controllers
 */
class GroupController extends AcpBaseController
{
    public function indexAction()
    {
        $page = PageFunction::getPageAndOffset($this->getParamValue('page', 1, 'int'), BusinessConstant::PAGE_SIZE);
        $name = $this->getParamValue('name', '');

        $conditions = [];

        if ($name) {
            $conditions['groupname%'] = $name;
        }

        $total = ManagerGroup::getInstance()->countListByConds($conditions);
        $list = [];
        $counts = [];
        if ($total > 0) {
            $list = ManagerGroup::getInstance()->getListByConds($conditions, $page['offset'], $page['size']);
            $gids = [];

            foreach ($list as $v) {
                $gids[] = $v->id;
            }

            $counts = Manager::getInstance()->countNumGroupByGroupId($gids);
        }

        $this->view->setVars([
            'list' => $list,
            'counts' => $counts,
            'pageHtml' => PageFunction::getPageHtml(
                $page['page'],
                $total,
                $page['size'],
                parse_url($this->request->getURI())['path'],
                $conditions,
                4
            )
        ]);
    }

    public function editGroupAction()
    {
        $id = $this->getParamValue('id', null, 'int');

        $data = ManagerGroup::getInstance()->getOneByField('id', $id);

        if (!$data) {
            static::errorThrow('未找到对应数据');
        }

        $privileges = explode(',', $data->privilege);

        $this->view->pick('Group/Addgroup');
        $this->view->setVars([
            'data' => $data,
            'modules' => AcpConstant::MODULES,
            'privileges' => $privileges
        ]);
    }

    public function deleteGroupAction()
    {
        ManagerGroup::getInstance()->deleteByField('id', $this->getParamValue('id', null, 'int'));

        parent::renderSuccessJson();
    }

    public function addGroupAction()
    {


        $this->view->setVars([
            'modules' => AcpConstant::MODULES,
            'data' => null,
            'privileges' => []
        ]);
    }

    public function saveAction()
    {

        $id = $this->getParamValue('id', 0, 'int');
        $groupName = $this->getParamValue('groupname');
        $privileges = $this->getParamValue('privilege');

        $values = [
            'groupname' => $groupName,
            'privilege' => implode(',', $privileges),
            'dateline' => time()
        ];

        //检查组名是否存在
        $tmp = ManagerGroup::getInstance()->getOneByGroupName($groupName);
        if ($tmp && ($id !== (int)$tmp->id)) {
            static::errorThrow('已存在同名用户组');
        }

        if ($id) {
            ManagerGroup::getInstance()->updateByFields(
                $values,
                [
                    'id' => $id
                ]
            );

            ManagerGroupService::getInstance()->cleanPrivilegesByGid($id);

        } else {
            ManagerGroup::getInstance()->insertByFields(
                $values
            );
        }

        parent::renderSuccessJson();
    }
}